'use strict';
const dayjs = require('dayjs');
module.exports = app => {
    const { INTEGER, STRING } = app.Sequelize;
    const UserPracticeRecord = app.model.define(
        'UserPracticeRecord',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            name: STRING,
            user_id: INTEGER,
            score: INTEGER,
            status: INTEGER,
            num: INTEGER,
            correct_num: INTEGER,
            category_id: INTEGER,
            course_id: INTEGER,
            minutes: INTEGER,
            total_score: INTEGER,
            is_lottery: INTEGER,
            start_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('start_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            end_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('end_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            created_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('created_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            updated_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'user_practice_record',
            timestamps: true, // 启用时间戳(createdAt, updatedAt)
            updatedAt: 'updated_time', // 不想要 updatedAt
            createdAt: 'created_time', // 想要  createdAt 但是希望名称叫做 created_at
        }
    );
    UserPracticeRecord.associate = function() {
        app.model.UserPracticeRecord.belongsTo(app.model.User, {foreignKey: 'user_id', targetKey: 'id'});
        app.model.UserPracticeRecord.belongsTo(app.model.Course, {foreignKey: 'course_id', targetKey: 'id'});
        app.model.UserPracticeRecord.belongsTo(app.model.CourseCategories, {foreignKey: 'category_id', targetKey: 'id'});
        app.model.UserPracticeRecord.hasMany(app.model.UserPractice, {foreignKey: 'record_id', sourceKey: 'id'});
    }
    return UserPracticeRecord;
};
